<template>
  <div>
    <el-dialog :title="title" :visible.sync="dialogFormVisible">
      <el-form :model="resourceData" v-loading="isLoading">
        <el-form-item label="接口路径" :label-width="formLabelWidth">
          <el-input v-model="resourceData.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="映射方法名称" :label-width="formLabelWidth">
          <el-input v-model="resourceData.mapperName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="请求方式" :label-width="formLabelWidth">
          <el-input v-model="resourceData.requestMethod" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="资源领域" :label-width="formLabelWidth">
          <el-select v-model="resourceData.sourceId" placeholder="请选择活动区域">
            <el-option v-for="item in sourceSelects" :label="item.sourceName" :value="item.id"
                       :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="resourceData.remark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="onsubmit()" :disabled="isLoading">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
//资源领域下拉列表接口
import {gainResourceSelects} from "@/api/resource/resource";
//资源名单详情接口
import {getByIdLimit, AddLimit, UpdateLimit} from '@/api/interfaceLimit/interfacelimit'

export default {
  name: "UpdateCreateInterfaceLimit",
  data() {
    return {
      isCreate: true,//标识:true创建 false编辑
      dialogFormVisible: false,
      sourceSelects: [],//下拉列表
      resourceData: {
        id: undefined,
        requestMethod: '',
        mapperName: '',
        url: '',
        remark: '',
        sourceId: '1',
      },
      //false加载完成 true正在加载
      isLoading: false,
      formLabelWidth: '120px'
    }
  },
  computed: {
    title() {
      return this.isCreate ? '新建资源' : '编辑资源'
    }
  },
  created() {

  },
  methods: {
    showAndInit(limitId) {
      this.dialogFormVisible = true;
      this.isCreate = limitId < 1;
      //下拉列表
      gainResourceSelects().then(res => {
        if (res.data.code == '200') {
          this.sourceSelects = res.data.data;
          console.log("UpdateCreateInterfaceLimit下拉列表:::", this.sourceSelects);
        }
      })
      //新建
      if (this.isCreate) {
        this.resourceData = {
          requestMethod: '',
          mapperName: '',
          url: '',
          remark: '',
          //默认选中第一个资源领域
          sourceId: '1',
        }
      }
      //编辑
      else {
        //资源名单详情
        this.isLoading = true
        getByIdLimit(limitId).then(res => {
          if (res.data.code == '200') {
            this.resourceData = res.data.data;
            console.log("资源名单详情:::", this.resourceData);
          }
        }).finally(() => {
          this.isLoading = false
        })
      }
    },
    //表单确定事件
    onsubmit() {
      // debugger
      this.isLoading = true
      //添加
      if (this.isCreate) {
        AddLimit(this.resourceData).then(res => {
          if (res.data.code == '200') {
            this.$message.success(`${this.title}成功!`)
          } else {
            this.$message.error(`${this.title}失败!`)
          }
        }).finally(() => {
          //关闭对话框
          this.dialogFormVisible = false
          //告诉父组件
          this.$emit('resource-change')
          this.isLoading = false
        })
      } else {
        UpdateLimit(this.resourceData).then(res => {
          if (res.data.code == '200') {
            this.$message.success(`${this.title}成功!`)
          } else {
            this.$message.error(`${this.title}失败!`)
          }
        }).finally(() => {
          //关闭对话框
          this.dialogFormVisible = false
          //告诉父组件
          this.$emit('resource-change')
          this.isLoading = false
        })
      }

    }
  }
}
</script>

<style >

</style>
